package com.tss.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tss.mapper.AttendanceMapper;
import com.tss.mapper.CourseMapper;
import com.tss.pojo.Attendance;
import com.tss.pojo.AttendanceExample;
import com.tss.pojo.AttendanceExample.Criteria;
import com.tss.pojo.Result;
import com.tss.service.AttendanceService;

@Service
public class AttendanceServiceImpl implements AttendanceService{

	@Autowired
	private AttendanceMapper attendanceMapper;
	
	@Autowired
	private CourseMapper courseMapper;
	
	@Override
	public Result createAttendance(Attendance attendance) {
		Result result = new Result();
		if(attendance==null||attendance.getCourseId()==null){
			result.setErrorMessage("参数错误！");
			return result;
		}
		if(courseMapper.selectByPrimaryKey(attendance.getCourseId())==null){
			result.setErrorMessage("课程不存在！");
			return result;
		}
		attendance.setValidate(true);
		attendanceMapper.insert(attendance);
		result.setSuccessMessage("创建签到表成功！");
		Attendance a = findAttendance(attendance);
		if(a!=null){
			result.addResult("id", a.getId());
		}
		return result;
	}

	@Override
	public Attendance findAttendance(Attendance attendance) {
		if(attendance==null){
			return null;
		}
		if(attendance.getId()!=null){
			Attendance a = attendanceMapper.selectByPrimaryKey(attendance.getId());
			if(a!=null&&a.getValidate()==true){
				return a;
			}else{
				return null;
			}
		}
		if(attendance.getCourseId()!=null){
			AttendanceExample example = new AttendanceExample();
			Criteria criteria = example.createCriteria();
			criteria.andValidateEqualTo(true);
			criteria.andCourseIdEqualTo(attendance.getCourseId());
			if(attendance.getSignInEndTime()!=null){
				criteria.andSignInEndTimeEqualTo(attendance.getSignInEndTime());
			}
			if(attendance.getSignInStartTime()!=null){
				criteria.andSignInStartTimeEqualTo(attendance.getSignInStartTime());
			}
			List<Attendance> list = attendanceMapper.selectByExample(example);
			if(!list.isEmpty()){
				return list.get(0);
			}
		}
		return null;
		
	}

	@Override
	public List<Attendance> listAttendances(Attendance attendance) {
		AttendanceExample example = new AttendanceExample();
		Criteria criteria = example.createCriteria();
		criteria.andValidateEqualTo(true);
		if(attendance==null){
			return attendanceMapper.selectByExample(example);
		}
		if(attendance.getCourseId()!=null){
			criteria.andCourseIdEqualTo(attendance.getCourseId());
		}
		return attendanceMapper.selectByExample(example);
	}

	@Override
	public Result deleteAttendance(Long id) {
		Result result = new Result();
		Attendance a = attendanceMapper.selectByPrimaryKey(id);
		if(a==null){
			return result.setErrorMessage("签到表不存在！");
		}
		a.setValidate(false);
		attendanceMapper.updateByPrimaryKey(a);
		return result.setSuccessMessage("删除签到表成功！");
	}
	

}
